(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.deliMessage.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'deliMessageService', 'commonService', 'storageService' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, deliMessageService, commonService, storageService) {
		var ctrl = this, info = $scope.$stateParams.params, log = $scope.loggerFactory.getLogger("deliMessageInboxList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.setupDataStatusOptions = commonService.setupDataStatusOptions();
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, serviceRowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			if (!ctrl.searchCriteria.deliveryFolder)
				ctrl.searchCriteria.deliveryFolder = 'R';// Received
			if (!ctrl.searchCriteria.deliveryType)
				ctrl.searchCriteria.deliveryType = 'H';// online (warehouse, online view only. no need to deliver)
			if (!ctrl.searchCriteria.toCompany && !ctrl.searchCriteria.fromCompany) {
				var workingCompanyCode = storageService.getUserInfo().workingCompanyCode ;
				ctrl.searchCriteria.toCompany = workingCompanyCode;
			}
			deliMessageService.initListSearchOptions($scope, ctrl).then(function(){
				ctrl.search();
			});
		};

		ctrl.reply = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.deliMessage.reply', ctrl.selectedRows[0]);
		};
		
		ctrl.create = function() {
			var params = {};
			$scope.go('cda.deliMessage.create', params);
		};
		
		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.deliMessage.view', ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function serviceRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);